<?php

class Je_YoutubeHelper 
{
	/**
	 * Returns an array of categories for the given extension.
	 *
	 * @param	string	The extension option.
	 * @param	array	An array of configuration options. By default, only published and unpulbished categories are returned.
	 *
	 * @return	array
	 */
	public static function categoryOptions($extension, $config = array('filter.published' => array(0,1)))
	{
		static $items = null;
		
		$hash = md5($extension.'.'.serialize($config));

		if (!isset($items[$hash])) {
			$config	= (array) $config;
			$db		= JFactory::getDbo();
			$query	= $db->getQuery(true);

			$query->select('a.id, a.title, a.level');
			$query->from('#__categories AS a');
			$query->where('a.parent_id > 0');

			// Filter on extension.
			$query->where('extension = '.$db->quote($extension));

			// Filter on the published state
			if (isset($config['filter.published'])) {
				if (is_numeric($config['filter.published'])) {
					$query->where('a.published = '.(int) $config['filter.published']);
				} else if (is_array($config['filter.published'])) {
					JArrayHelper::toInteger($config['filter.published']);
					$query->where('a.published IN ('.implode(',', $config['filter.published']).')');
				}
			}

			$query->order('a.lft');

			$db->setQuery($query);
			$categories = $db->loadObjectList();

			// Assemble the list options.
			$items[$hash] = array();

			foreach ($categories as &$item) {				
				$repeat = ( $item->level - 1 >= 0 ) ? $item->level - 1 : 0;
				#$item->title = str_repeat('- ', $repeat).$item->title;
				
				$obj 			= new stdClass();
				$obj->value 	= $item->id;
				$obj->text 		= $item->title;
				$obj->disabled 	= false;
				$obj->level 	= $item->level;
				
				$items[$hash][] = $obj;
			}
		}

		return $items[$hash];
	}
}